home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / fileutil.13 / fileutil / fileutils-3.13 / src / rmdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-24  |  3.6 KB  |  155 lines

  1. /* rmdir -- remove directories
  2.    Copyright (C) 90, 91, 95, 1996 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software Foundation,
  16.    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  17.  
  18. /* Options:
  19.    -p, --parent        Remove any parent dirs that are explicitly mentioned
  20.             in an argument, if they become empty after the
  21.             argument file is removed.
  22.  
  23.    David MacKenzie <djm@ai.mit.edu>  */
  24.  
  25. #include <config.h>
  26. #include <stdio.h>
  27. #include <getopt.h>
  28. #include <sys/types.h>
  29.  
  30. #include "system.h"
  31. #include "error.h"
  32.  
  33. void strip_trailing_slashes ();
  34.  
  35. /* The name this program was run with. */
  36. char *program_name;
  37.  
  38. /* If nonzero, remove empty parent directories. */
  39. static int empty_paths;
  40.  
  41. /* If nonzero, display usage information and exit.  */
  42. static int show_help;
  43.  
  44. /* If nonzero, print the version on standard output and exit.  */
  45. static int show_version;
  46.  
  47. static struct option const longopts[] =
  48. {
  49.   {"path", no_argument, &empty_paths, 1},
  50.   {"parents", no_argument, &empty_paths, 1},
  51.   {"help", no_argument, &show_help, 1},
  52.   {"version", no_argument, &show_version, 1},
  53.   {NULL, 0, NULL, 0}
  54. };
  55.  
  56. /* Remove any empty parent directories of `path'.
  57.    Replaces '/' characters in `path' with NULs. */
  58.  
  59. static void
  60. remove_parents (char *path)
  61. {
  62.   char *slash;
  63.  
  64.   do
  65.     {
  66.       slash = strrchr (path, '/');
  67.       if (slash == NULL)
  68.     break;
  69.       /* Remove any characters after the slash, skipping any extra
  70.      slashes in a row. */
  71.       while (slash > path && *slash == '/')
  72.     --slash;
  73.       slash[1] = 0;
  74.     }
  75.   while (rmdir (path) == 0);
  76. }
  77.  
  78. static void
  79. usage (int status)
  80. {
  81.   if (status != 0)
  82.     fprintf (stderr, _("Try `%s --help' for more information.\n"),
  83.          program_name);
  84.   else
  85.     {
  86.       printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name);
  87.       printf (_("\
  88. Remove the DIRECTORY(ies), if they are empty.\n\
  89. \n\
  90.   -p, --parents   remove explicit parent directories if being emptied\n\
  91.       --help      display this help and exit\n\
  92.       --version   output version information and exit\n"));
  93.     }
  94.   exit (status);
  95. }
  96.  
  97. int
  98. main (int argc, char **argv)
  99. {
  100.   int errors = 0;
  101.   int optc;
  102.  
  103.   program_name = argv[0];
  104.   setlocale (LC_ALL, "");
  105.   bindtextdomain (PACKAGE, LOCALEDIR);
  106.   textdomain (PACKAGE);
  107.  
  108.   empty_paths = 0;
  109.  
  110.   while ((optc = getopt_long (argc, argv, "p", longopts, (int *) 0)) != EOF)
  111.     {
  112.       switch (optc)
  113.     {
  114.     case 0:            /* Long option. */
  115.       break;
  116.     case 'p':
  117.       empty_paths = 1;
  118.       break;
  119.     default:
  120.       usage (1);
  121.     }
  122.     }
  123.  
  124.   if (show_version)
  125.     {
  126.       printf ("rmdir - %s\n", PACKAGE_VERSION);
  127.       exit (0);
  128.     }
  129.  
  130.   if (show_help)
  131.     usage (0);
  132.  
  133.   if (optind == argc)
  134.     {
  135.       error (0, 0, _("too few arguments"));
  136.       usage (1);
  137.     }
  138.  
  139.   for (; optind < argc; ++optind)
  140.     {
  141.       /* Stripping slashes is harmless for rmdir;
  142.      if the arg is not a directory, it will fail with ENOTDIR.  */
  143.       strip_trailing_slashes (argv[optind]);
  144.       if (rmdir (argv[optind]) != 0)
  145.     {
  146.       error (0, errno, "%s", argv[optind]);
  147.       errors = 1;
  148.     }
  149.       else if (empty_paths)
  150.     remove_parents (argv[optind]);
  151.     }
  152.  
  153.   exit (errors);
  154. }
  155.